home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #18 (Mar 87) / Printer LSP sources / WindowsStuff < prev   
Text File  |  1987-02-13  |  5KB  |  187 lines

  1. UNIT Windows;
  2.  
  3. INTERFACE
  4.  
  5.     USES
  6.         MacPrint, MyGlobals, DumpTPrint;
  7.  
  8.     PROCEDURE CheckWindowMode;
  9.     PROCEDURE CloseAWindow;
  10.     PROCEDURE DialogueDeactivate;
  11.     PROCEDURE DrawWindow;
  12.     PROCEDURE MyActivate;
  13.     PROCEDURE OpenAWindow;
  14.  
  15. IMPLEMENTATION
  16.  
  17. { Windows segment }
  18.  
  19. {-----------Update menus based on windows---------}
  20.     PROCEDURE CheckWindowMode;
  21.         VAR
  22.             newmode : MyWindMode;
  23.             fileset : SET OF 1..lastFileItem;
  24.             item : INTEGER;
  25.     BEGIN    { This routine enable/disables menu items based on window mode }
  26.         myPeek := WindowPeek(FrontWindow);
  27.         IF myPeek = NIL THEN
  28.             newmode := NullMode        { no windows open }
  29.         ELSE IF myPeek^.windowKind = MyDocument THEN
  30.             newmode := OpenMode        { document window on top }
  31.         ELSE
  32.             newmode := DAMode;        { assume must be D.A. on top }
  33.         IF newmode <> currWMode THEN    { Must change menus }
  34.             BEGIN
  35.                 CASE newmode OF
  36.                     NullMode :        { No windows open }
  37.                         fileset := [newItem, quitItem];
  38.                     OpenMode :        { One window open and on top }
  39.                         fileset := [closeItem, stlItem, jobItem, setupItem, printItem, quitItem];
  40.                     DAMode :        { DA on top }
  41.                         fileset := [closeItem, quitItem];
  42.                     OTHERWISE
  43.                         BEGIN
  44.                         END;
  45.                 END; {CASE newmode}
  46.                 FOR item := 1 TO lastFileItem DO
  47.                     IF item IN fileset THEN
  48.                         EnableItem(myMenus[fileMenu], item)
  49.                     ELSE
  50.                         DisableItem(myMenus[fileMenu], item);
  51.  
  52.                 IF newmode = DAMode THEN
  53.                     EnableItem(myMenus[editMenu], 0)
  54.                 ELSE
  55.                     DisableItem(myMenus[editMenu], 0);
  56.  
  57.                 DrawMenuBar;        { menu dimming must be updated }
  58.                 currWMode := newmode;
  59.             END; {IF newmode <> currWMode}
  60.     END; {CheckWindowMode}
  61.  
  62. {----------Close the front window----------}
  63.     PROCEDURE CloseAWindow;
  64.     BEGIN
  65. { This routine closes an application (or DA) window, either after}
  66. {    • clicking go-away box                    }
  67. {    • selecting "Close" in File menu            }
  68.         myPeek := WindowPeek(FrontWindow);
  69.         IF myPeek^.windowKind = myDocument THEN
  70.             BEGIN
  71.                 wdh := WindowDataHandle(GetWRefCon(WindowPtr(myPeek)));
  72.                 ph := wdh^^.theTHP;
  73.                 DisposHandle(Handle(ph));
  74.                 TEDispose(hTE);
  75.                 hTE := NIL;
  76.                 DisposHandle(Handle(wdh));
  77.                 DisposeWindow(myWindow);
  78.             END {myDocument window}
  79.         ELSE { Must be a DA }
  80.             CloseDeskAcc(myPeek^.windowKind)
  81.     END; {CloseAWindow}
  82.  
  83. {-----------------Deactivate before dialog----------}
  84. {Deactivate the top window if we're about to put up a dialog}
  85.     PROCEDURE DialogueDeactivate;
  86.         VAR
  87.             temprect : Rect;
  88.     BEGIN
  89.         SetCursor(arrow);
  90.         IF hTE <> NIL THEN {for documents, only}
  91.             TEDeactivate(hTE);
  92.     END; {DialogueDeactivate}
  93.  
  94. {---------Draw a document window----------}
  95. { Handles window Update Event}
  96.     PROCEDURE DrawWindow;
  97.         VAR
  98.             tempport : GrafPtr;
  99.             temprect, rectToErase : Rect;
  100.             temppeek : WindowPeek;
  101.             whichwindow : WindowPtr;
  102.             temphTE : TEHandle;
  103.     BEGIN
  104.         whichwindow := WindowPtr(myEvent.message);
  105.         BeginUpdate(whichwindow);
  106.         GetPort(tempport);
  107.         SetPort(whichwindow);
  108.         temppeek := WindowPeek(whichwindow);
  109.         IF temppeek^.windowKind = myDocument THEN
  110.             BEGIN
  111.                 temprect := whichwindow^.Portrect;
  112.                 wdh := WindowDataHandle(GetWRefCon(whichwindow));
  113.                 temphTE := wdh^^.theTE;
  114.                 SetRect(temprect, -32767, -32767, 32767, 32767);
  115.                 ClipRect(temprect);
  116. {this only erases the window past the end of text, if any}
  117.                 WITH temphTE^^ DO
  118.                     IF nLines < (viewRect.bottom - viewRect.top + lineHeight) DIV lineHeight THEN
  119.                         BEGIN
  120.                             rectToErase := viewRect;
  121.                             rectToErase.top := (nLines) * lineHeight;
  122.                             EraseRect(rectToErase)
  123.                         END; {nLines}
  124.                 TEUpdate(whichwindow^.visRgn^^.rgnBBox, temphTE)
  125.             END; {myDocument stuff}
  126.         SetPort(tempport);
  127.         EndUpdate(whichwindow)
  128.     END; {DrawWindow}
  129.  
  130. {-------------Handle (de)activate events-----------}
  131.     PROCEDURE MyActivate;
  132.  
  133.     BEGIN    {This activates or deactivates the current selection}
  134.         myWindow := WindowPtr(myEvent.message);
  135.         myPeek := WindowPeek(myWindow);
  136.         IF myPeek^.windowKind = myDocument THEN
  137.             BEGIN        { document window }
  138.                 wdh := WindowDataHandle(GetWRefCon(myWindow));
  139.                 hTE := wdh^^.theTE;
  140.                 IF ODD(myEvent.modifiers) THEN { BitAnd(myEvent.modifiers,activeFlag)>0 }
  141.                     TEActivate(hTE)    {this window is now top most}
  142.                 ELSE            {this window is no longer top most}
  143.                     BEGIN
  144.                         TEDeactivate(hTE);
  145.                         hTE := NIL    {a TextEdit window is no longer on top}
  146.                     END;
  147.             END;
  148.  
  149.     END; {MyActivate}
  150.  
  151. {-------------Create a new document window-------}
  152.     PROCEDURE OpenAWindow;
  153.  
  154.         VAR
  155.             r : Rect;
  156.  
  157.     BEGIN    {A window is created here}
  158.  
  159.         myWindow := GetNewWindow(WIND_main, NIL, Pointer(-1));
  160.         wdh := WindowDataHandle(NewHandle(SIZEOF(WindowData)));
  161.         SetWRefCon(myWindow, ORD(wdh));    { stash pointer to TEHandle in window }
  162.  
  163.         SetPort(myWindow);
  164.         myPeek := WindowPeek(myWindow);
  165.         TextFont(myStdFont);
  166.         TextSize(myStdSize);
  167.         DrawChar(' ');
  168.         SetFontLock(TRUE);
  169.         myPeek^.windowKind := myDocument; {identifies the type of window}
  170.  
  171.         r := myWindow^.Portrect;
  172.         InsetRect(r, 8, 4);
  173.         hTE := TENew(r, r);
  174.         wdh^^.theTE := hTE;
  175.         hTE^^.destRect := hTE^^.viewRect;
  176.         hTE^^.crOnly := -1;        { no automatic CR }
  177.  
  178.         PrOpen;
  179.         ph := THPrint(NewHandle(SIZEOF(TPrint)));
  180.         PrintDefault(ph);
  181.         wdh^^.theTHP := ph;
  182.         DumpPrint('After PrintDefault(…)', ph);
  183.         PrClose;
  184.  
  185.     END; {OpenAWindow}
  186.  
  187. END.